<template>
  <div class="register-container">
    <div class="register-box">
      <h2>注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label>邮箱：</label>
          <input 
            type="email" 
            v-model="email" 
            required 
            placeholder="请输入邮箱"
          >
        </div>
        <div class="form-group">
          <label>密码：</label>
          <input 
            type="password" 
            v-model="password" 
            required 
            placeholder="请输入密码"
          >
        </div>
        <div class="form-group">
          <label>确认密码：</label>
          <input 
            type="password" 
            v-model="confirmPassword" 
            required 
            placeholder="请再次输入密码"
          >
        </div>
        <button type="submit">注册</button>
        <div class="links">
          <router-link to="/">已有账号？立即登录</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const email = ref('')
const password = ref('')
const confirmPassword = ref('')

// TODO: API - 用户注册
const handleRegister = async () => {
  if (password.value !== confirmPassword.value) {
    alert('两次输入的密码不一致')
    return
  }

  try {
    // await request.post('/api/register', {
    //   email: email.value,
    //   password: password.value
    // })
    
    // 模拟注册成功
    alert('注册成功！')
    router.push('/login')
  } catch (error) {
    alert('注册失败：该邮箱已被注册')
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.register-box {
  width: 400px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.form-group {
  margin-bottom: 15px;
}

label {
  display: block;
  margin-bottom: 5px;
}

input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

.links {
  margin-top: 15px;
  text-align: center;
}

a {
  color: #4CAF50;
  text-decoration: none;
}
</style>
